package com.example.hybridmobiledemo.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.hybridmobiledemo.module.main.model.shop.bean.ShopCartInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MyClaDataAdapter$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 24/10/2021$
 * @Version: 1.0
 */
public class MyClaDataAdapter {
    private ClaMySQLiteOpenHelper openHelper;
    private SQLiteDatabase database;

    public void openDB(Context context){
        openHelper = new ClaMySQLiteOpenHelper(context, "ClaDB.db", null, 1);
        try{
            database = openHelper.getWritableDatabase();
        } catch (Exception e) {
            database = openHelper.getReadableDatabase();
        }
    }

    public void closeDB() {
        if (database != null) {
            database.close();
            database = null;
        }
        if (openHelper != null) {
            openHelper.close();
            openHelper = null;
        }
    }

    public List<ShopCartInfo> Query(Context context, int mId) {
        List<ShopCartInfo> cartList = new ArrayList<>();
        openDB(context);
        Cursor cursor = database.query("ClaCom", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String commodityId = cursor.getString(1);
                String commodityName = cursor.getString(2);
                float costPrice = cursor.getFloat(4);
                int num = cursor.getInt(6);
                String pic = cursor.getString(7);
                if (id == mId) {
                }
                cartList.add(new ShopCartInfo(commodityId, commodityName, costPrice, num, pic));
            } while (cursor.moveToNext());
        }
        cursor.close();
        closeDB();
        return cartList;
    }

    public List<ShopCartInfo> QueryAll(Context context) {
        List<ShopCartInfo> cartList = new ArrayList<>();
        openDB(context);
        Cursor cursor = database.query("ClaCom", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String commodityId = cursor.getString(1);
                String commodityName = cursor.getString(2);
                float costPrice = cursor.getFloat(4);
                int num = cursor.getInt(6);
                String pic = cursor.getString(7);
                cartList.add(new ShopCartInfo(commodityId, commodityName, costPrice, num, pic));
            } while (cursor.moveToNext());
        }
        cursor.close();
        closeDB();
        return cartList;
    }

    public long insert(Context context, ShopCartInfo shopCartInfo) {
        openDB(context);
        ContentValues values = new ContentValues();
        values.put("commodityId", shopCartInfo.getCommodityId());
        values.put("commodityName", shopCartInfo.getCommodityName());
        values.put("commodityTypeName", shopCartInfo.getTypeName());
        values.put("costPrice", shopCartInfo.getfCostPrice());
        values.put("money", shopCartInfo.getfMoney());
        values.put("num", shopCartInfo.getNum());
        values.put("pic", shopCartInfo.getPic());
        long result = database.insert("ClaCom", null, values);
        closeDB();
        return result;
    }

    public int delete(Context context, String id) {
        openDB(context);
        int result = database.delete("ClaCom", "commodityId=?", new String[]{id});
        closeDB();
        return result;
    }

    public int update(Context context, String id, ShopCartInfo shopCartInfo) {
        openDB(context);
        ContentValues values = new ContentValues();
        values.put("commodityId", shopCartInfo.getCommodityId());
        values.put("commodityName", shopCartInfo.getCommodityName());
        values.put("commodityTypeName", shopCartInfo.getTypeName());
        values.put("costPrice", shopCartInfo.getfCostPrice());
        values.put("money", shopCartInfo.getfMoney());
        values.put("num", shopCartInfo.getNum());
        values.put("pic", shopCartInfo.getPic());
        int result = database.update("ClaCom", values, "commodityId=?", new String[]{id});
        closeDB();
        return result;
    }

}
